package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Customer;
import com.ruoyi.APPMS.service.ICustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 顾客管理Controller
 *
 * @author tianq
 * @date 2023-07-03
 */
@Api(tags = "顾客信息管理")
@RestController
@RequestMapping("/customer/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询顾客管理列表
     */
    @ApiOperation("分页查询顾客列表")
    @PreAuthorize("@ss.hasPermi('customer:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer)
    {

        //name phone
        startPage();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        if (customer.getCustomername()!= null && !"".equals(customer.getCustomername())){
            queryWrapper.like("customername",customer.getCustomername());
        }
        if (customer.getContactnumber()!= null && !"".equals(customer.getContactnumber())){
            queryWrapper.like("contactnumber",customer.getContactnumber());
        }
        if (customer.getEmail()!= null && !"".equals(customer.getEmail())){
            queryWrapper.like("email",customer.getEmail());
        }
        if (customer.getAddress()!= null && !"".equals(customer.getAddress())){
            queryWrapper.like("address",customer.getAddress());
        }

        List<Customer> list = customerService.list(queryWrapper);
//        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 导出顾客管理列表
     */
    @ApiOperation("导出选中的顾客列表")
    @PreAuthorize("@ss.hasPermi('customer:customer:export')")
    @Log(title = "顾客管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customer customer)
    {

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        if (customer.getCustomername()!= null && !"".equals(customer.getCustomername())){
            queryWrapper.like("customername",customer.getCustomername());
        }
        if (customer.getContactnumber()!= null && !"".equals(customer.getContactnumber())){
            queryWrapper.like("contactnumber",customer.getContactnumber());
        }
        if (customer.getEmail()!= null && !"".equals(customer.getEmail())){
            queryWrapper.like("email",customer.getEmail());
        }
        if (customer.getAddress()!= null && !"".equals(customer.getAddress())){
            queryWrapper.like("address",customer.getAddress());
        }
        List<Customer> list = customerService.list(queryWrapper);

        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "顾客管理数据");
    }

    /**
     * 获取顾客管理详细信息
     */
    @ApiOperation("查询顾客详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerid",value = "顾客编号",required = true),
    })
    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping(value = "/{custormerid}")
    public AjaxResult getInfo(@PathVariable("custormerid") Long custormerid)
    {
        return success(customerService.getById(custormerid));
//        return success(customerService.selectCustomerByCustormerid(custormerid));
    }


    /**
     * 新增顾客管理
     */
    @ApiOperation("新增顾客")
    @PreAuthorize("@ss.hasPermi('customer:customer:add')")
    @Log(title = "顾客管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer)
    {
        customer.setDeleted(0L);
        customer.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(customerService.save(customer));
    }

    /**
     * 修改顾客管理
     */
    @ApiOperation("修改顾客")
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "顾客管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer)
    {
        customer.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(customerService.updateById(customer));
    }

    /**
     * 删除顾客管理
     */
    @ApiOperation("删除顾客")
    @PreAuthorize("@ss.hasPermi('customer:customer:remove')")
    @Log(title = "顾客管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{custormerids}")
    public AjaxResult remove(@PathVariable List<Long> custormerids)
    {
        return toAjax(customerService.removeByIds(custormerids));
    }

    @ApiOperation("查询所有顾客")
    @PreAuthorize("@ss.hasPermi('customer:customer:getcustomersList')")
    @Log(title = "查询所有顾客数据，初始化下拉列表", businessType = BusinessType.DELETE)
    @GetMapping("/getcustomersList")
    public AjaxResult getcustomersList(){
        return AjaxResult.success(customerService.list());
    }
}
